package com.collect.cameroon.zhiScene.zhiWidget.zhiFilterDaily;

import android.annotation.SuppressLint;
import android.content.Context;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.collect.cameroon.R;
import com.collect.cameroon.zhiAdater.zhiFilterDaily.ZHIFilterConditionAdjuster;
import com.collect.cameroon.zhiAdater.zhiFilterPerformance.ZHIFilterLevelAdjuster;
import com.collect.cameroon.zhiBean.zhiReceive.ZHIRFilterEntity;
import com.collect.cameroon.zhiHelper.ZHIMainProcessHelper;
import com.collect.cameroon.zhiScene.zhiWidget.zhiFilterAdminGroup.ZHIRangePicker;
import com.collect.cameroon.zhiUtils.StringUtil;

import java.util.ArrayList;

@SuppressLint("NonConstantResourceId")
public class ZHIFilterDaily extends RelativeLayout {

    private final Context mContext;

    private String mOrderType;

    private RecyclerView rc_order_type_list;
    private RecyclerView rc_call_type_list;
    private RecyclerView rc_call_result_list;

    private ZHIRangeDateSelect rds_promised_repay_time;
    private ZHIRangeDateSelect rds_due_date;
    private ZHIRangeDaySelect rds_overdue_days;
    private ZHIRangeMobileInput rds_user_mobile;

    private ZHIRangePicker rds_member;

    private ZHIFilterConditionAdjuster mZHIFilterOrderTypeAdjuster;
    private ZHIFilterConditionAdjuster mZHIFilterCallTypeAdjuster;
    private ZHIFilterConditionAdjuster mZHIFilterCallResultAdjuster;
    private ZHIFilterLevelAdjuster mZHIFilterPartRepayAdjuster;

    private ArrayList<ZHIRFilterEntity> mOrderTypeFilterArrayList;
    private ArrayList<ZHIRFilterEntity> mCallTypeFilterArrayList;
    private ArrayList<ZHIRFilterEntity> mCallResultFilterArrayList;
    private ArrayList<ZHIRFilterEntity> mPartRepayFilterArrayList;

    private ZHIFilterDailyClickListener mZHIFilterDailyClickListener;

    public ZHIFilterDaily(Context context) {
        this(context, null);
    }

    public ZHIFilterDaily(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZHIFilterDaily(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        this.mContext = context;

        initView();
    }

    @SuppressLint("NotifyDataSetChanged")
    private void initView() {
        try {
            View mView = View.inflate(mContext, R.layout.view_filter_daily, this);

            rc_order_type_list = mView.findViewById(R.id.rc_order_type_list);
            rc_call_type_list = mView.findViewById(R.id.rc_call_type_list);
            rc_call_result_list = mView.findViewById(R.id.rc_call_result_list);

            rds_promised_repay_time = mView.findViewById(R.id.rds_promised_repay_time);
            rds_due_date = mView.findViewById(R.id.rds_due_date);
            rds_overdue_days = mView.findViewById(R.id.rds_overdue_days);
            rds_user_mobile = mView.findViewById(R.id.rds_user_mobile);

            rds_member = mView.findViewById(R.id.rds_member);

            TextView tv_reset = mView.findViewById(R.id.tv_reset);
            tv_reset.setOnClickListener(v -> {
                for (ZHIRFilterEntity zhirFilterEntity1 : mOrderTypeFilterArrayList) {
                    zhirFilterEntity1.setChecked(zhirFilterEntity1.getLevel().equalsIgnoreCase(mContext.getResources().getString(R.string.today_all_orders)));
                }
                mOrderType = mContext.getResources().getString(R.string.today_all_orders);
                mZHIFilterOrderTypeAdjuster.notifyDataSetChanged();

                for (ZHIRFilterEntity zhirFilterEntity1 : mCallTypeFilterArrayList) {
                    zhirFilterEntity1.setChecked(zhirFilterEntity1.getLevel().equalsIgnoreCase(mContext.getResources().getString(R.string.no_call)));
                }
                mZHIFilterCallTypeAdjuster.notifyDataSetChanged();

                for (ZHIRFilterEntity zhirFilterEntity1 : mCallResultFilterArrayList) {
                    zhirFilterEntity1.setChecked(zhirFilterEntity1.getLevel().equalsIgnoreCase("-1"));
                }
                mZHIFilterCallResultAdjuster.notifyDataSetChanged();

                for (ZHIRFilterEntity zhirFilterEntity1 : mPartRepayFilterArrayList) {
                    zhirFilterEntity1.setChecked(zhirFilterEntity1.getLevel().equalsIgnoreCase(mContext.getResources().getString(R.string.today_all_orders)));
                }
                mZHIFilterPartRepayAdjuster.notifyDataSetChanged();

                rds_promised_repay_time.reset();
                rds_due_date.reset();
                rds_overdue_days.reset();
                rds_user_mobile.reset();
                rds_member.reset();

            });

            RelativeLayout rl_close_bg = mView.findViewById(R.id.rl_close_bg);
            rl_close_bg.setOnClickListener(v -> {
                if (mZHIFilterDailyClickListener != null) {
                    mZHIFilterDailyClickListener.onCloseClick();
                }
            });

            TextView tv_submit = mView.findViewById(R.id.tv_submit);
            tv_submit.setOnClickListener(v -> {
                if (mZHIFilterDailyClickListener != null) {
                    mZHIFilterDailyClickListener.onConfirmClick();
                }
            });

            ////////////////////////////////////
            initOrderTypeAdjuster();

            ////////////////////////////////////
            initCallTypeAdjuster();

            ////////////////////////////////////
            initCallResultAdjuster();

            ///////////////////////
            RecyclerView rc_part_repay_list = mView.findViewById(R.id.rc_part_repay_list);
            initPartRepayDataSource();

            mZHIFilterPartRepayAdjuster = new ZHIFilterLevelAdjuster();
            mZHIFilterPartRepayAdjuster.setOnZHIFilterAdjusterItemClickListener((zhirFilterEntity, position) -> {
                if (zhirFilterEntity != null) {
                    for (ZHIRFilterEntity zhirFilterEntity1 : mPartRepayFilterArrayList) {
                        zhirFilterEntity1.setChecked(zhirFilterEntity1.getLevel().equalsIgnoreCase(zhirFilterEntity.getLevel()));
                    }
                    mZHIFilterPartRepayAdjuster.notifyDataSetChanged();
                }
            });
            rc_part_repay_list.setLayoutManager(new GridLayoutManager(mContext, 3));
            rc_part_repay_list.setAdapter(mZHIFilterPartRepayAdjuster);
            mZHIFilterPartRepayAdjuster.setNewData(mPartRepayFilterArrayList);

            ////////////////////////////////////
            rds_member.setDataSource(initUserDataSource());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initOrderTypeAdjuster() {
        String[] datas = new String[]{
                mContext.getResources().getString(R.string.today_all_orders),
                mContext.getResources().getString(R.string.today_new_orders),
                mContext.getResources().getString(R.string.today_repay_orders)};

        String[] dataValues = new String[]{
                "TOTAL",
                "ADD",
                "REPAY"};

        mOrderTypeFilterArrayList = new ArrayList<>();
        for (int i = 0; i < datas.length; i++) {
            ZHIRFilterEntity zhirFilterEntity = new ZHIRFilterEntity();
            zhirFilterEntity.setLevel(datas[i]);
            zhirFilterEntity.setDateValue(dataValues[i]);
            zhirFilterEntity.setChecked(i == 0);
            mOrderTypeFilterArrayList.add(zhirFilterEntity);
        }

        mZHIFilterOrderTypeAdjuster = new ZHIFilterConditionAdjuster();
        mZHIFilterOrderTypeAdjuster.setOnZHIFilterAdjusterItemClickListener((zhirFilterEntity, position) -> {
            if (zhirFilterEntity != null) {
                for (ZHIRFilterEntity zhirFilterEntity1 : mOrderTypeFilterArrayList) {
                    if (zhirFilterEntity1.getLevel().equals(zhirFilterEntity.getLevel())) {
                        zhirFilterEntity1.setChecked(!zhirFilterEntity1.isChecked());

                    } else {
                        zhirFilterEntity1.setChecked(false);
                    }
                }
                mZHIFilterOrderTypeAdjuster.notifyDataSetChanged();
            }
        });
        rc_order_type_list.setLayoutManager(new GridLayoutManager(mContext, 3));
        rc_order_type_list.setAdapter(mZHIFilterOrderTypeAdjuster);
        mZHIFilterOrderTypeAdjuster.setNewData(mOrderTypeFilterArrayList);
    }

    private void initCallTypeAdjuster() {
        String[] datas = new String[]{
                mContext.getResources().getString(R.string.no_call),
                mContext.getResources().getString(R.string.call_once),
                mContext.getResources().getString(R.string.call_twice)};

        String[] dataValues = new String[]{
                "NOTMARK",
                "ONEMARK",
                "TWOMARK"};

        mCallTypeFilterArrayList = new ArrayList<>();
        for (int i = 0; i < datas.length; i++) {
            ZHIRFilterEntity zhirFilterEntity = new ZHIRFilterEntity();
            zhirFilterEntity.setLevel(datas[i]);
            zhirFilterEntity.setDateValue(dataValues[i]);
            zhirFilterEntity.setChecked(i == 0);
            mCallTypeFilterArrayList.add(zhirFilterEntity);
        }

        mZHIFilterCallTypeAdjuster = new ZHIFilterConditionAdjuster();
        mZHIFilterCallTypeAdjuster.setOnZHIFilterAdjusterItemClickListener((zhirFilterEntity, position) -> {
            if (zhirFilterEntity != null) {
                for (ZHIRFilterEntity zhirFilterEntity1 : mCallTypeFilterArrayList) {
                    if (zhirFilterEntity1.getLevel().equals(zhirFilterEntity.getLevel())) {
                        zhirFilterEntity1.setChecked(!zhirFilterEntity1.isChecked());
                    } else {
                        zhirFilterEntity1.setChecked(false);
                    }

                }
                mZHIFilterCallTypeAdjuster.notifyDataSetChanged();
            }
        });
        rc_call_type_list.setLayoutManager(new GridLayoutManager(mContext, 3));
        rc_call_type_list.setAdapter(mZHIFilterCallTypeAdjuster);
        mZHIFilterCallTypeAdjuster.setNewData(mCallTypeFilterArrayList);
    }

    @SuppressLint("NotifyDataSetChanged")
    private void initCallResultAdjuster() {
        String[] datas = new String[]{
                mContext.getResources().getString(R.string.call_result_ptp),
                mContext.getResources().getString(R.string.call_result_rnr),
                mContext.getResources().getString(R.string.call_result_fake),
                mContext.getResources().getString(R.string.call_result_switched_off),
                mContext.getResources().getString(R.string.call_result_innvalid_num),
                mContext.getResources().getString(R.string.call_result_out_of_coverage),
                mContext.getResources().getString(R.string.call_result_msg_sent),
                mContext.getResources().getString(R.string.call_result_paid),
                mContext.getResources().getString(R.string.call_result_connected)};

        mCallResultFilterArrayList = new ArrayList<>();
        for (int i = 0; i < datas.length; i++) {
            ZHIRFilterEntity zhirFilterEntity = new ZHIRFilterEntity();
            zhirFilterEntity.setLevel(datas[i]);
            zhirFilterEntity.setDateValue(datas[i]);
            zhirFilterEntity.setChecked(false);
            mCallResultFilterArrayList.add(zhirFilterEntity);
        }

        mZHIFilterCallResultAdjuster = new ZHIFilterConditionAdjuster();
        mZHIFilterCallResultAdjuster.setOnZHIFilterAdjusterItemClickListener((zhirFilterEntity, position) -> {
            if (zhirFilterEntity != null) {
                for (ZHIRFilterEntity zhirFilterEntity1 : mCallResultFilterArrayList) {
                    if (zhirFilterEntity1.getLevel().equals(zhirFilterEntity.getLevel())) {
                        zhirFilterEntity1.setChecked(!zhirFilterEntity1.isChecked());
                    } else {
                        zhirFilterEntity1.setChecked(false);
                    }
                }
                mZHIFilterCallResultAdjuster.notifyDataSetChanged();
            }
        });
        rc_call_result_list.setLayoutManager(new GridLayoutManager(mContext, 3));
        rc_call_result_list.setAdapter(mZHIFilterCallResultAdjuster);
        mZHIFilterCallResultAdjuster.setNewData(mCallResultFilterArrayList);
    }

    private void initPartRepayDataSource() {
        try {
            if (mPartRepayFilterArrayList != null) {
                mPartRepayFilterArrayList.clear();
            } else {
                mPartRepayFilterArrayList = new ArrayList<>();
            }

            ZHIRFilterEntity zhirFilterEntity0 = new ZHIRFilterEntity();
            zhirFilterEntity0.setLevel("ALL");
            zhirFilterEntity0.setDateValue("");
            zhirFilterEntity0.setChecked(true);
            mPartRepayFilterArrayList.add(zhirFilterEntity0);

            ZHIRFilterEntity zhirFilterEntity1 = new ZHIRFilterEntity();
            zhirFilterEntity1.setLevel("YES");
            zhirFilterEntity1.setDateValue("YES");
            zhirFilterEntity1.setChecked(false);
            mPartRepayFilterArrayList.add(zhirFilterEntity1);

            ZHIRFilterEntity zhirFilterEntity2 = new ZHIRFilterEntity();
            zhirFilterEntity2.setLevel("NO");
            zhirFilterEntity2.setDateValue("NO");
            zhirFilterEntity2.setChecked(false);
            mPartRepayFilterArrayList.add(zhirFilterEntity2);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public ArrayList<ZHIRangeDay> initUserDataSource() {
        ArrayList<ZHIRangeDay> arrayList = new ArrayList<>();
        try {
            ArrayList<String> userList = ZHIMainProcessHelper.getInstance().getLoginEntity().getGroupUserLoginNameList();
            for (int i = 0; i < userList.size(); i++) {
                ZHIRangeDay zhiRangeDay = new ZHIRangeDay();
                zhiRangeDay.setRangeId(i + "");
                zhiRangeDay.setDayTitle(userList.get(i));
                arrayList.add(zhiRangeDay);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return arrayList;
    }

    public void setZHIFilterDailyClickListener(ZHIFilterDailyClickListener listener) {
        this.mZHIFilterDailyClickListener = listener;
    }

    public void setDisableMemberSelect(boolean disableMemberSelect) {
        rds_member.mOnlyShowDefaultValue = disableMemberSelect;
    }

    public void setDefaultMember(String member) {
        rds_member.updateCompare(false, member);
    }

    public String getOrderType() {

        String tResult = "";
        if (mOrderTypeFilterArrayList != null) {
            for (ZHIRFilterEntity zhirFilterEntity : mOrderTypeFilterArrayList) {
                if (zhirFilterEntity.isChecked()) {
                    tResult = zhirFilterEntity.getDateValue();
                    break;
                }
            }
        }

        return tResult;
    }

    public String getCallType() {
        String tResult = "";
        if (mCallTypeFilterArrayList != null) {
            for (ZHIRFilterEntity zhirFilterEntity : mCallTypeFilterArrayList) {
                if (zhirFilterEntity.isChecked()) {
                    tResult = zhirFilterEntity.getDateValue();
                    break;
                }
            }
        }
        return tResult;
    }

    public String getCallResult() {
        String tResult = "";
        if (mCallResultFilterArrayList != null) {
            for (ZHIRFilterEntity zhirFilterEntity : mCallResultFilterArrayList) {
                if (zhirFilterEntity.isChecked()) {
                    tResult = zhirFilterEntity.getDateValue();
                    break;
                }
            }
        }
        return tResult;
    }

    public String getPromisedRepayStartDate() {
        return StringUtil.safeString(rds_promised_repay_time.getStartDate());
    }

    public String getPromisedRepayEndDate() {
        return StringUtil.safeString(rds_promised_repay_time.getEndDate());
    }

    public String getDudStartDate() {
        return StringUtil.safeString(rds_due_date.getStartDate());
    }

    public String getDueEndDate() {
        return StringUtil.safeString(rds_due_date.getEndDate());
    }

    public String getCompareFlag() {
        String tempStr = StringUtil.safeString(rds_overdue_days.getCompare());
        String tflag = "";

        if (getResources().getString(R.string.more_than).equalsIgnoreCase(tempStr)) {
            tflag = ">";
        } else if (getResources().getString(R.string.equal).equalsIgnoreCase(tempStr)) {
            tflag = "=";
        } else if (getResources().getString(R.string.less_than).equalsIgnoreCase(tempStr)) {
            tflag = "<";
        }

        return tflag;
    }

    public String getCompareOverdueDays() {
        return StringUtil.safeString(rds_overdue_days.getDays());
    }

    public String getUserMobile() {
        return StringUtil.safeString(rds_user_mobile.getMobile());
    }

    public String getMember() {
        return StringUtil.safeString(rds_member.getCompare());
    }


    public String getPartRepay() {
        String partReapy = "";
        if (mPartRepayFilterArrayList != null) {
            for (int i = 0; i < mPartRepayFilterArrayList.size(); i++) {
                ZHIRFilterEntity zhirFilterEntity = mPartRepayFilterArrayList.get(i);
                if (zhirFilterEntity.isChecked()) {
                    partReapy = zhirFilterEntity.getDateValue();
                    break;
                }
            }
        }

        return partReapy;
    }

    public void setPartRepay(String partRepay) {
        try {
            if (mPartRepayFilterArrayList != null) {
                partRepay = StringUtil.safeString(partRepay);

                for (int i = 0; i < mPartRepayFilterArrayList.size(); i++) {
                    ZHIRFilterEntity zhirFilterEntity = mPartRepayFilterArrayList.get(i);
                    zhirFilterEntity.setChecked(zhirFilterEntity.getLevel().equalsIgnoreCase(partRepay));
                }

                if (mZHIFilterPartRepayAdjuster != null) {
                    mZHIFilterPartRepayAdjuster.setNewData(mPartRepayFilterArrayList);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
